package com.vcms.utility.loader.storage.engine.file.impl;

import com.vcms.entity.store.item.CashStoreItem;
import com.vcms.entity.store.itemtype.Coin;
import com.vcms.exception.VMCSException;
import com.vcms.utility.loader.storage.engine.file.FileStorageEngine;

/**
 * @author robin.foe A0092657U
 * Bridge design pattern concrete implementor
 */
public class CashFileStorageEngine extends FileStorageEngine<CashStoreItem>{

	public static final String PROPERTY_NAME = "Name";
	public static final String PROPERTY_WEIGHT = "Weight";
	public static final String PROPERTY_VALUE = "Value";
	public static final String PROPERTY_QUANTITY = "Quantity";
	
	public CashFileStorageEngine(String fileName) throws VMCSException {
		super(fileName);
	}

	@Override
	public CashStoreItem getItem(int pk) {
		Coin coin = new Coin();
		coin.setName(super.getValueFromProperty(PROPERTY_NAME + pk));
		coin.setWeight(Double.parseDouble ( super.getValueFromProperty(PROPERTY_WEIGHT + pk) ) );
		coin.setValue(Integer.parseInt( super.getValueFromProperty(PROPERTY_VALUE + pk)));
		return new CashStoreItem(coin, Integer.parseInt( super.getValueFromProperty (PROPERTY_QUANTITY + pk)));
	}
	
	public void setItem(int pk, CashStoreItem cashStoreItem){
		Coin coin = (Coin)cashStoreItem.getContent();
		super.setValueToProperty(PROPERTY_NAME + pk, coin.getName());
		super.setValueToProperty(PROPERTY_WEIGHT + pk, String.valueOf(coin.getWeight()));
		super.setValueToProperty(PROPERTY_VALUE + pk, String.valueOf(coin.getValue()));
		super.setValueToProperty(PROPERTY_QUANTITY + pk,  String.valueOf(cashStoreItem.getQuantity()) );
		
	}

}
